{% extends 'base.html' %}

{% block head %}
	<script type="text/javascript">
		document.observe("dom:loaded", function(){
			//Variables:
			//var form_element = $('table_form');
			//var temp_arr = form_element.getInputs();
			//alert('WHAAAT');
			var size = $('id_size');
			var balance = $('id_balance');
			var debt = $('id_debt');
			var balance_view = $('id_balance_view');
			var debt_view = $('id_debt_view');
			var size_old = $('id_sizeOld');
			//Mill logic
			var noOfMills = {{ numberOfMills }};
			var i = 1;
			var WindmillArray = new Array();
			for(i = 1; i<=noOfMills; i++){
				var temp = new Array();
				temp[0] = $('id_windmillName'+i);
				//temp[0].writeAttribute('disabled', 'disabled');
				temp[0].up().up().hide();
				temp[1] = $('id_number'+i);
				temp[2] = temp[1].getValue();
				temp[3] = $('id_costReal'+i);
				temp[3].up().up().hide();
				temp[4] = $('id_numberOriginal'+i);
				temp[4].up().up().hide();
				temp[5] = $('id_numberNew'+i);
				temp[5].up().up().hide();
				temp[6] = $('id_maintenanceOld'+i);
				temp[6].up().up().hide();
				temp[7] = $('id_maintenance'+i);
				temp[8] = temp[7].getValue();
				WindmillArray[i] = temp;
			}

			//Hide two of them, to keep a value in a non-disabled field
			balance_view.up().up().hide();
			debt_view.up().up().hide();
			size_old.up().up().hide();
			//Disable two fields, to keep a value that can be showed
			balance.writeAttribute('disabled', 'disabled');
			debt.writeAttribute('disabled', 'disabled');
			//Write beginning balances en debts
			balance.writeAttribute('value', '{{ balance }}');
			balance_view.writeAttribute('value', '{{ balance }}');
			debt.writeAttribute('value', '{{ debt }}');
			debt_view.writeAttribute('value', '{{ debt }}');
			size.writeAttribute('value', '{{ capacity }}');
			size_old.writeAttribute('value', '{{ capacity }}');
			//Add some elements with text
			
			var table = $('table_form');
			var myArray = table.getElementsBySelector('tr');
			myArray.each(function(item){
				var input = item.getElementsBySelector('input')[0];
				if (input.readAttribute('name') != null){
					var new_td = new Element('td')
					if (input.readAttribute('name') == 'size'){
						new_td.update('in MWh. 1 MWh kost 100.000 euro.');
					}
					if (input.readAttribute('name') == 'balance'){
						new_td.update('Het bedrag op je bankrekening in euro\'s.');
					}
					if (input.readAttribute('name') == 'debt'){
						new_td.update('De uitstaande lening in euro\'s.');
					}
					if ((input.readAttribute('name').match(/windmill/i) == 'windmill' ) && (input.visible())){
						new_td.update('In de volgende 5 velden staat informatie over dit type windmolen.');
					}
					if ((input.readAttribute('name').match(/number/i) == 'number' ) && (input.visible())){
						new_td.update('Vul in hoeveel windmolens van dit type worden aangekocht.');
					}
					if ((input.readAttribute('name').match(/maintenance/i) == 'maintenance' ) && (input.visible())){
						new_td.update('Vul in hoeveel nieuwe windmolens een onderhoudscontract krijgen.');
					}
					if ((input.readAttribute('name').match(/capacity/i) == 'capacity' ) && (input.visible())){
						new_td.update('Vermogen van dit type windmolen in MW.');
					}
					if ((input.readAttribute('name').match(/costShow/i) == 'costShow' ) && (input.visible())){
						new_td.update('Aankoopprijs van dit type windmolen in euro\'s.');
					}
					if ((input.readAttribute('name').match(/diameter/i) == 'diameter' ) && (input.visible())){
						new_td.update('Diameter van dit type windmolen.');
					}
					item.insert(new_td);
				}
			});
			//Two more variables, needed for the observers
			var start_balance = balance.getValue();
			var start_debt = debt.getValue();
			var start_size = size.getValue();
			var start_mills = 0;
			WindmillArray.each(function(item){
				start_mills = start_mills + item[2];
			});
			//var start_mills = mills.getValue();
			//observers:
			new Form.Element.Observer(size, 0.1, function(){
				//Reflect changes in Island Size:
				var new_balance = start_balance-((size.getValue()-start_size)*100000);
				//on enlargement, newSize gets bigger
				var newSize = size.getValue()-size_old.getValue();
				var newPrice = newSize*100000;
				if (newPrice >= 0){
					if ((1*start_balance) <= 0){
						var newDebt = (1*start_debt) + newPrice;
						var newBalance = 0;
					}else{ //Debt was equal to 0
						var newBalance = (1*start_balance) - newPrice;
						var newDebt = 0;
						if (newBalance < 0){
							newDebt = (1*start_debt) + (-1 * newBalance);
							newBalance = 0;
						}
					}
				} else { //newPrice is negative, so island is made smaller
					if ((1*start_debt) > 0){
						var newDebt = (1*start_debt) + newPrice;
						var newBalance = 0;
						if (newDebt <= 0){
							newBalance = (1*start_balance) + (-1 * newDebt);
							newDebt = 0;
						}
					} else { //only Balance was used
						var newBalance = (1*start_balance) - newPrice;
						var newDebt = 0;
					}
				}

				//Also take number of windmills in account!
				var mill_price = 0;
				WindmillArray.each(function(item){
					var number = item[1].getValue()-item[2];
					var price = item[3].getValue();
					var total = number*price;
					mill_price = mill_price + total;
				});
				
				if (newBalance > 0){
					if ((newBalance - mill_price) > 0){
						newBalance = newBalance - mill_price;
					} else{
						newDebt = -1 * (newBalance - mill_price);
						newBalance = 0;
					}
				} else { //use debt
					newDebt = newDebt + mill_price;
					if (newDebt < 0){
						newBalance = -1 * newDebt;
						newDebt = 0;
					}
				}
				
				//Also take number of maintenance windmills in account!
				var maintenance_price = 0;
				WindmillArray.each(function(item){
					var number = item[7].getValue()-item[8];
					var price = 0.02 * item[3].getValue(); // 2% of purchase price
					var total = price * number;
					maintenance_price = maintenance_price + total;
				});
				
				if (newBalance > 0){
					if ((newBalance - maintenance_price) > 0){
						newBalance = newBalance - maintenance_price;
					} else{
						newDebt = -1 * (newBalance - maintenance_price);
						newBalance = 0;
					}
				} else { //use debt
					newDebt = newDebt + maintenance_price;
					if (newDebt < 0){
						newBalance = -1 * newDebt;
						newDebt = 0;
					}
				}
				
				balance.writeAttribute('value', newBalance);
				balance_view.writeAttribute('value', newBalance);
				debt.writeAttribute('value', newDebt);
				debt_view.writeAttribute('value', newDebt);
			});
			WindmillArray.each(function(item){
				new Form.Element.Observer(item[1], 0.1, function(){
					//Reflect changes in Island Size:
					item[5].writeAttribute('value', item[1].getValue()-item[4].getValue());
					var new_balance = start_balance-((size.getValue()-start_size)*100000);
					//on enlargement, newSize gets bigger
					var newSize = size.getValue()-size_old.getValue();
					var newPrice = newSize*100000;
					if (newPrice >= 0){
						if ((1*start_balance) <= 0){
							var newDebt = (1*start_debt) + newPrice;
							var newBalance = 0;
						}else{ //Debt was equal to 0
							var newBalance = (1*start_balance) - newPrice;
							var newDebt = 0;
							if (newBalance < 0){
								newDebt = (1*start_debt) + (-1 * newBalance);
								newBalance = 0;
							}
						}
					} else { //newPrice is negative, so island is made smaller
						if ((1*start_debt) > 0){
							var newDebt = (1*start_debt) + newPrice;
							var newBalance = 0;
							if (newDebt <= 0){
								newBalance = (1*start_balance) + (-1 * newDebt);
								newDebt = 0;
							}
						} else { //only Balance was used
							var newBalance = (1*start_balance) - newPrice;
							var newDebt = 0;
						}
					}
					//Also take number of windmills in account!
					var mill_price = 0;
					WindmillArray.each(function(item){
						var number = item[1].getValue()-item[2];
						var price = item[3].getValue();
						var total = number*price;
						mill_price = mill_price + total;
					});
					
					if (newBalance > 0){
						if ((newBalance - mill_price) > 0){
							newBalance = newBalance - mill_price;
						} else{
							newDebt = -1 * (newBalance - mill_price);
							newBalance = 0;
						}
					} else { //use debt
						newDebt = newDebt + mill_price;
						if (newDebt < 0){
							newBalance = -1 * newDebt;
							newDebt = 0;
						}
					}
					
					//Also take number of maintenance windmills in account!
					var maintenance_price = 0;
					WindmillArray.each(function(item){
						var number = item[7].getValue()-item[8];
						var price = 0.02 * item[3].getValue(); // 2% of purchase price
						var total = price * number;
						maintenance_price = maintenance_price + total;
					});
					
					if (newBalance > 0){
						if ((newBalance - maintenance_price) > 0){
							newBalance = newBalance - maintenance_price;
						} else{
							newDebt = -1 * (newBalance - maintenance_price);
							newBalance = 0;
						}
					} else { //use debt
						newDebt = newDebt + maintenance_price;
						if (newDebt < 0){
							newBalance = -1 * newDebt;
							newDebt = 0;
						}
					}
					
					balance.writeAttribute('value', newBalance);
					balance_view.writeAttribute('value', newBalance);
					debt.writeAttribute('value', newDebt);
					debt_view.writeAttribute('value', newDebt);				
				});
				new Form.Element.Observer(item[7], 0.1, function(){
					//Reflect changes in Island Size:
				
					var new_balance = start_balance-((size.getValue()-start_size)*100000);
					//on enlargement, newSize gets bigger
					var newSize = size.getValue()-size_old.getValue();
					var newPrice = newSize*100000;
					if (newPrice >= 0){
						if ((1*start_balance) <= 0){
							var newDebt = (1*start_debt) + newPrice;
							var newBalance = 0;
						}else{ //Debt was equal to 0
							var newBalance = (1*start_balance) - newPrice;
							var newDebt = 0;
							if (newBalance < 0){
								newDebt = (1*start_debt) + (-1 * newBalance);
								newBalance = 0;
							}
						}
					} else { //newPrice is negative, so island is made smaller
						if ((1*start_debt) > 0){
							var newDebt = (1*start_debt) + newPrice;
							var newBalance = 0;
							if (newDebt <= 0){
								newBalance = (1*start_balance) + (-1 * newDebt);
								newDebt = 0;
							}
						} else { //only Balance was used
							var newBalance = (1*start_balance) - newPrice;
							var newDebt = 0;
						}
					}
					//Also take number of windmills in account!
					var mill_price = 0;
					WindmillArray.each(function(item){
						var number = item[1].getValue()-item[2];
						var price = item[3].getValue();
						var total = number*price;
						mill_price = mill_price + total;
					});
					
					if (newBalance > 0){
						if ((newBalance - mill_price) > 0){
							newBalance = newBalance - mill_price;
						} else{
							newDebt = -1 * (newBalance - mill_price);
							newBalance = 0;
						}
					} else { //use debt
						newDebt = newDebt + mill_price;
						if (newDebt < 0){
							newBalance = -1 * newDebt;
							newDebt = 0;
						}
					}
					
					//Also take number of maintenance windmills in account!
					var maintenance_price = 0;
					WindmillArray.each(function(item){
						var number = item[7].getValue()-item[8];
						var price = 0.02 * item[3].getValue(); // 2% of purchase price
						var total = price * number;
						maintenance_price = maintenance_price + total;
					});
					
					if (newBalance > 0){
						if ((newBalance - maintenance_price) > 0){
							newBalance = newBalance - maintenance_price;
						} else{
							newDebt = -1 * (newBalance - maintenance_price);
							newBalance = 0;
						}
					} else { //use debt
						newDebt = newDebt + maintenance_price;
						if (newDebt < 0){
							newBalance = -1 * newDebt;
							newDebt = 0;
						}
					}
					
					balance.writeAttribute('value', newBalance);
					balance_view.writeAttribute('value', newBalance);
					debt.writeAttribute('value', newDebt);
					debt_view.writeAttribute('value', newDebt);
				});
			});
			
		});
		
	</script>
{% endblock head %}

{% block title %}Nieuwe Ronde{% endblock title %}

{% block user_menu %}

	{% if is_groep %}
		<li><a href="{{ BASE_PATH }}news/home/{{ game_id }}">Nieuws</a></li>
		<li><a href="{{ BASE_PATH }}round/home/{{ game_id }}">Resultaten</a></li>
		<li><a href="{{ BASE_PATH }}round/home/{{ game_id }}">Overzicht</a></li>
		<li><a href="{{ BASE_PATH }}round/edit/{{ game_id }}">Beslissingen</a></li>
	{% endif %}

<li><a href="{{ BASE_PATH }}logout">Uitloggen?</a></li>
{% endblock user_menu %}

{% block body%}
	<div class="post">
		<div class="post-bgtop">
			<div class="post-bgbtm">
				<h1 class="title"> Nieuwe ronde </h1>
				<div class="entry">
					<form enctype="multipart/form-data" method="post" action="{{ BASE_PATH }}round/new/{{ game_id }}" id="form">
						<table id="table_form">
							{{ form }}
							<tr><td><input type="submit" value="Save" /></td></tr>
						</table>
					</form>
				</div>
			</div>
		</div>
	</div>

{% endblock body %}
